﻿<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:ScriptLink name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
</asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    
    <script src="../Scripts/jquery-1.9.1.min.js"></script>
    <script>
        var oList;
        function createList()
          {
            var listname = $('input[id=listname]').val();
            var clientContext = new SP.ClientContext.get_current();
            var oWebsite = clientContext.get_web();

            var listCreationinfo = new SP.ListCreationInformation();
            listCreationinfo.set_title(listname);
            listCreationinfo.set_templateType(SP.ListTemplateType.announcements);

            oList = oWebsite.get_lists().add(listCreationinfo);
            clientContext.load(oList);
           
            clientContext.executeQueryAsync(qs, qf);
        }
        function deleteList()
        {
            var listname = $('input[id=listname]').val();
            var clientContext = new SP.ClientContext.get_current();
            var oWebsite = clientContext.get_web();
            this.oList = oWebsite.get_lists().getByTitle(listname);
            this.oList.deleteObject();
            clientContext.executeQueryAsync(qs, qf);
        }

        //returns the list items from list
        function GetListItems()
        {
            var listname = $('input [id=listname]').val();
            //alert(listname);
            //listname="Expense"

            //alert(listname);
            //laistname(siteurl);
            var clientContext = new SP.ClientContext.get_current();
            var camlQ = new SP.CamlQuery();
            camlQ.set_viewXml('<view><RowLimit>100</Limit></view>');

            var oweb = clientContext.get_web();
            var olists = oweb.get_lists();
            var olists = olists.getByTitle(listname);

            this.colListItem = olist.get_Items(camlQ);
            clientContext.load(colListItem);
            clientContext.executeQueryAsync(qs_listItem, qf_listItem);


        }

        // creat list items
        function CreatListItem()
        {
            var listname = $('input[id=listname]').val();
            var clientcontext = new SP.ClientContext.get_current();
            var oList = clientcontext.get_web().get_lists().getByTitle(listname);

            var itemCreatInfo = new SP.ListItemCreationinformation();
            this.oList = oList.addIttem(itemCreateInfo);

            oListItem.set_Item('Title', 'Title created!');
            //oListItem.set_item('Body' , 'Body created!');

            oListItem.update();
            clientcontext.executeQueryAsync(qs_lic, qf_lic);
        }

        function qs_liupdate(sender,args)
        {
            alert('Item updated :'+ oListItem.get_item('Title'));

        }
        function qf_(sender,args)
        {
            alert('Request Failed.' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function qs_lic(sender , args)
        {
            alert('item Created :' + oListItem.get_id());
        }
        function qf_lic(sender ,args)
        {
            alert ('Request failed.' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function qs_listItem(sender , args)
        {
            var listiteminfo = '';
            varliEnum = collistitem.getEnumerator();

            while (Enum.moveNext)
            {
                var oLi = liEnum.get_current();
                listiteminfo += '\nID:' + oLi.get_id() + '\nTitle:' + oLi.get_item('Title') + '\nBody:' + oLi.get_item('Body');
            }
            alert(listiteminfo);
        }
        function qf_listitem(sender,args)
        {
            alert('Request failed.'+ args.get_message()+ '\n'+args.get_stackTrace());
        }
        //function on Qurey Succedded(sendr,args)

        function qs(sender,args)
        {
            var result = oList.get_title()+'creatd';
            alert(result)
        }
        //function on Query failed(sender,args)
        function qf(sender,args)
        {
            alert('Request failed.' + args.get_message() + '\n' + args.get_stackTrace());
        }

    </script>

    <br />
    <br />

    <br />
    <br />

    ListName:<input type="text" id="listname" /><br />
    Item Id to work on:<input type="text" id="itemid" /><br /><br />
    <button type="button" onclick="createList()">Creat List</button><br />
    <button type="button" onclick="deleteList()">Delete List</button><br />
    <button type="button" onclick="GetListItems()">Get List Items</button><br />
    <button type="button" onclick="CreatListItem()">Creat List Item</button><br />
    Note* Creats a List Item with Title column value equal to "Title"
    <br />
    <br />
    <button type="button" onclick="UpdateListItem()">Update List Item</button><br />
    Note* Updates the specified (in list name text box item) list's Title column's value to "Tittle
    <br />
    <br />


    


    <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="full" Title="loc:full" />
</asp:Content>
